<template>
  <div class="page-container">
    <div class="common-content-wrap">
      <TrTable
        ref="table"
        isNeedIndex
        :total="totalNum"
        v-model="searchParams"
        :searchShowList="searchShowList"
        :searchHideList="searchHideList"
        :tableColumn="tableColumn"
        requestName="getListHandle"
        :tableData="tableData"
        @view="operateHandle($event, 'view')"
      />
    </div>

    <TrDetail
      v-model="isShowFristPopup"
      popupTitle="查看详情"
      :leftList="fristDetail"
      statusModel="hidden"
      :width="1227"
      :label-width="fristWidth"
      :detailData="fristData"
      @close="isShowFristPopup = false"
    >
      <template #bomTable>
        <TrTable
          isPopup
          isNeedIndex
          v-model="fristSearchParams"
          :tableData="fristTable"
          :total="fristTotal"
          :tableColumn="fristColunm"
          requestName="getDetailHandle"
          @viewDetail="operateHandle($event, 'viewDetail')"
        />
      </template>
    </TrDetail>
    <TrDetail
      v-model="isShowSecondPopup"
      popupTitle="查看详情"
      :leftList="secondDetail"
      statusModel="hidden"
      :width="1227"
      :label-width="secondWidth"
      :detailData="secondData"
      @close="isShowSecondPopup = false"
    >
      <template #bomTable>
        <TrTable
          isPopup
          isNeedIndex
          v-model="secondSearchParams"
          :tableData="secondTable"
          :total="secondTotal"
          :tableColumn="secondColunm"
          :expandObj="secondExpandObj"
          requestName="getSecondDetailHandle"
        />
      </template>
    </TrDetail>
  </div>
</template>

<script>
import { getObject } from "./js/orderFirstDetailLevel";
import { getColor } from "@/utils/billColor";
import {
  getAllList,
  getDetail,
  getParticulars,
} from "@/api/logistics/orderSearch";
import TrTable from "@/components/Tr-table/Tr-table.vue";
import TrDetail from "@/components/Tr-detail/Tr-detail.vue";

export default {
  name: "orderSearch",
  components: {
    TrTable,
    TrDetail,
  },
  data: () => ({
    tableColumn: [
      {
        type: "text",
        label: "单据编号",
        value: "billCode",
      },
      {
        type: "text",
        label: "单据类型",
        value: "billTypeStrName",
      },
      {
        type: "text",
        label: "状态",
        value: "stateStr",
      },
      {
        type: "text",
        label: "创建人员",
        value: "createUserName",
      },
      {
        type: "text",
        label: "创建时间",
        value: "createTime",
      },
      {
        type: "option",
        label: "操作",
        fixed: "right",
        width: 80,
        showBtnList: [
          {
            label: "查看",
            option: "view",
          },
        ],
      },
    ],
    searchHideList: [
      {
        id: "billCode",
        type: "input",
        width: 33.3,
        label: "单据编号",
        value: "",
      },
      {
        id: "createUserName",
        type: "input",
        width: 33.3,
        label: "创建人员",
        value: "",
      },
    ],
    searchShowList: [
      {
        isNeed: true,
        startStr: "createStartTime",
        endStr: "createEndTime",
        id: "createdTime",
        type: "daterange",
        width: 80,
        label: "创建时间",
        value: [],
        timePeriod: true,
        radio: 1,
      },
    ],
    tableData: [],
    searchParams: {
      current: 1,
      size: 10,
    },
    totalNum: 0,

    isShowFristPopup: false,
    fristTotal: 0,
    fristColunm: [],
    fristData: {},
    fristTable: [],
    fristDetail: [],
    fristSearchParams: {
      current: 1,
      size: 10,
      billId: "",
      billType: "",
    },
    isShowSecondPopup: false,
    secondTotal: 0,
    secondColunm: [],
    secondData: {},
    secondTable: [],
    secondDetail: [],
    secondExpandObj: {},
    secondSearchParams: {
      current: 1,
      size: 10,
      id: "",
      billType: "",
    },
    fristWidth: 72,
    secondWidth: 72,
    transferType: null,
  }),
  mounted() {
    // this.getListHandle();
  },
  methods: {
    operateHandle(val, type) {
      console.log(val, type, "oooooo");
      if (type == "view") {
        this.transferType = val.transferType;
        const valueObj = getObject(val.billType, val.transferType);
        if (!valueObj) return this.$message.warning("暂未找到该单据类型!");
        this.fristSearchParams.billType = val.billType;
        this.fristSearchParams.billId = val.billId;
        this.fristDetail = valueObj.firstDetailList;
        this.fristColunm = valueObj.firstTableColumn;
        this.fristWidth = valueObj.firstWidth;
        this.getDetailHandle();
      } else if (type == "viewDetail") {
        const valueObj = getObject(
          this.fristSearchParams.billType,
          this.transferType
        );
        if (!valueObj) return this.$message.warning("暂未找到该单据类型!");
        this.secondSearchParams.billType = this.fristSearchParams.billType;
        this.secondSearchParams.id = val.id || val.bizDetailId;
        this.secondSearchParams.materialId = val.materialId || "";
        this.secondSearchParams.lineNo = val.lineNo || "";
        this.secondDetail = valueObj.secondDetailList;
        this.secondColunm = valueObj.secondTableColumn;
        this.secondWidth = valueObj.secondWidth;
        this.secondExpandObj =
          this.fristSearchParams.billType == 40 ||
          this.fristSearchParams.billType == 100
            ? valueObj.expandObj
            : {};
        this.getSecondDetailHandle();
      }
    },
    async getDetailHandle() {
      const { data } = await getDetail(this.fristSearchParams);
      if (data.code !== 200) return;
      this.$validateData(data.data);
      let dataObj = data.data;
      let result;
      let stateFlag = null;
      let stateValue = "state";
      switch (dataObj.billType) {
        case 10:
          result = dataObj.readyBizDetailDetailRespPageResp;
          stateFlag = "stateName";
          break;
        case 20:
          result = dataObj.scrapBizBillDetailPcPageRespPageResp;
          stateFlag = "stateStr";
          break;
        case 30:
          result = dataObj.enterWhBillQueryDetailResp;
          stateFlag = "bizBillStateDesc";
          stateValue = "bizBillState";
          break;
        case 40:
          result = dataObj.completionWhBizQueryBillDetailResp;
          stateFlag = "stateName";
          break;
        case 50:
          result = dataObj.miscOutDetailQueryBillResp;
          result.receptUserName = `${result.useUserDeptName}/${result.useUserName}`;
          if (result.state != 40 && result.state != 50) {
            this.fristColunm = this.fristColunm.slice(0, -1);
          }
          break;
        case 60:
          result = dataObj.transferBillDetailResp;
          stateFlag = "stateName";
          if (result.state == 10 || result.state == 20) {
            this.fristColunm = this.fristColunm.slice(0, -1);
          }
          break;
        case 70:
          result = dataObj.refundBizBillResp;
          stateFlag = "stateName";
          break;
        case 80:
          result = dataObj.logisticsBusDetailQueryBillResp;
          stateFlag = "stateName";
          break;
        case 90: {
          result = dataObj.twiceMaterialQueryBillResp;
          this.fristTable.forEach((item) => {
            item["stateNameColor"] =
              {
                10: "#ED7B2F",
                20: "#2F7AED",
                30: "#00A870",
              }[item.state] || "";
          });
          stateFlag = "stateStr";
          break;
        }
        case 100:
          result = dataObj.productShipmentDetailQueryBillResp;
          if (result.state == 10 || result.state == 20) {
            this.fristColunm = this.fristColunm.slice(0, -1);
          }
          break;
      }
      if (stateFlag) {
        const fItem = this.$findByProperty(
          this.fristDetail,
          "id",
          stateFlag,
          "list"
        );
        if (fItem)
          fItem.valueColor = this.getColorValue(
            dataObj.billType,
            result[stateValue]
          );
      }
      this.fristData = result;
      this.fristTable = result.pageDetailResp.records;
      this.fristTable.forEach((item) => {
        item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
        item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
        if (item.purcUnit !== undefined) {
          item.newPurcUnit = `${item.purcUnit}(${item.purcUnitName})`;
        }
      });
      this.fristTotal = result.pageDetailResp.total;
      this.secondSearchParams.bizBillId = result.bizBillId || "";
      this.isShowFristPopup = true;
    },
    async getSecondDetailHandle() {
      const { data } = await getParticulars(this.secondSearchParams);
      if (data.code !== 200) return;
      this.$validateData(data.data);
      let dataObj = data.data;
      let result;
      switch (dataObj.billType) {
        case 10:
          result = dataObj.pickBizDetailDetailResp;
          break;
        case 20:
          result = dataObj.scrapBillQueryParticularPcPageResp;
          break;
        case 30:
          result = dataObj.enterWhBillQueryParticularResp;
          break;
        case 40:
          result = dataObj.completionWhBizDetailQueryParticularResp;
          break;
        case 50:
          result = dataObj.miscOutParticularPageResp;
          break;
        case 60:
          result = dataObj.transferDetailParticularResp;
          break;
        case 70:
          result = dataObj.refundParticularResp;
          break;
        case 80:
          result = dataObj.logisticsBillParticularResp;
          break;
        case 90:
          result = dataObj.twinBillQueryParticularPageResp;
          break;
        case 100:
          result = dataObj.productShipmentBarcodeQueryBillResp;
          break;
      }
      this.secondData = result;
      this.secondData.newNumUnit = `${result.numUnitCode}(${result.numUnitName})`;
      this.secondData.newWeightUnit = `${result.weightUnitCode}(${result.weightUnitName})`;
      this.secondTable = result.pageParticularsResp.records;
      this.secondTotal = result.pageParticularsResp.total;
      if (result.purcUnit !== undefined) {
        this.secondData.newPurcUnit = `${result.purcUnit}(${result.purcUnitName})`;
      }
      if (result.receivedNumUnit !== undefined) {
        this.secondData.newReceivedNumUnit = `${result.receivedNumUnit}(${result.receivedNumUnitName})`;
      }
      if (result.receivedWeightUnit !== undefined) {
        this.secondData.newReceivedWeightUnit = `${result.receivedWeightUnit}(${result.receivedWeightUnitName})`;
      }
      this.secondTable.forEach((item) => {
        item.newNumUnit = `${item.numUnitCode}(${item.numUnitName})`;
        item.newWeightUnit = `${item.weightUnitCode}(${item.weightUnitName})`;
        if (item.labelContainerList) {
          item.labelContainerList.forEach((val) => {
            val.newNumUnit = `${val.numUnitCode}(${val.numUnitName})`;
            val.newWeightUnit = `${val.weightUnitCode}(${val.weightUnitName})`;
          });
        }
        if (item.numUnit !== undefined) {
          item.newNumUnit = `${item.numUnit}(${item.numUnitName})`;
        }
        if (item.weightUnit !== undefined) {
          item.newWeightUnit = `${item.weightUnit}(${item.weightUnitName})`;
        }
      });
      this.isShowSecondPopup = true;
    },
    async getListHandle() {
      const { data } = await getAllList(this.searchParams);
      if (data.code != 200) return this.$message.error(data.msg);
      this.$validateData(data.data);
      this.tableData = data.data.records;
      this.totalNum = data.data.total;
      this.tableData.forEach((item) => {
        item["stateStrColor"] = this.getColorValue(item.billType, item.state);
        item["billTypeStrName"] =
          item.billType == 80 ? item.transferTypeName : item.billTypeStr;
      });
    },
    getColorValue(type, state) {
      switch (type) {
        case 10:
          return getColor("LLD", state);
        case 20:
        case 30:
        case 40:
        case 70:
        case 80:
        case 90:
          return getColor("DF", state);
        case 50:
          return getColor("ZXCK", state);
        case 60:
          return getColor("DBD", state);
        case 100:
          return getColor("CPCK", state);
        default:
          return getColor("CPCK", state);
      }
    },
  },
};
</script>
